package br.cs.correspodente.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.cs.correspodente.beans.Financiamento;
import br.cs.correspodente.beans.Vendedor;
import br.cs.correspodente.conexao.FactoryConnection;
import br.cs.correspodente.interfaces.DAO;
import br.cs.correspodente.util.FinanciamentoUtil;
import br.cs.correspodente.util.VendedorUtil;

public class FinacimentoDAO implements DAO<Financiamento>{
	
	private final static String sqlAdd = "INSERT INTO Financimanto VALUES(0,?,?,?,?,?,?,?,?);";
	private final static String sqlDell = "DELETE FROM Financimanto WHERE idFinancimanto = ?;";
	private final static String sqlUp = "UPDATE Financimanto SET idComprador=?,valor_compra=?valor_financiamento=?,valor_parcela=?,saldo_devedor=?,prazo=?,recursos=?,status=? WHERE idFinancimento = ?;";
	private final static String sqlAll = "SELECT *FROM Financimanto WHERE idVendedor = ?;";
	private final static String sqlGetByID = "SELECT *FROM Financimanto WHERE idVendedor = ?;";
	
	@Override
	public void add(Financiamento financimento) throws SQLException,
			ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlAdd);
	//	stm.setInt(1, financimento.getIdComprador());
		stm.setFloat(2, financimento.getValor_Compra());
		stm.setFloat(3, financimento.getValor_Financimanto());
		stm.setFloat(4, financimento.getValor_Parcela());
		stm.setFloat(5, financimento.getSaldo_Devedor());
		stm.setInt(6, financimento.getPrazo());
		stm.setString(7, financimento.getRecursos());
		stm.setString(8, financimento.getStatus());
		stm.execute();
		FactoryConnection.getInstance().closeConexao(con, stm);
		
	}

	@Override
	public void dell(int id) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlDell);
		stm.setInt(1,id);
		stm.execute();
		FactoryConnection.getInstance().closeConexao(con, stm);
		
	}

	@Override
	public void up(Financiamento financimento) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlUp);
		//stm.setInt(1, financimento.getIdComprador());
		stm.setFloat(2, financimento.getValor_Compra());
		stm.setFloat(3, financimento.getValor_Financimanto());
		stm.setFloat(4, financimento.getValor_Parcela());
		stm.setFloat(5, financimento.getSaldo_Devedor());
		stm.setInt(6, financimento.getPrazo());
		stm.setString(7, financimento.getRecursos());
		stm.setString(8, financimento.getStatus());
		stm.setInt(9, financimento.getId());
		stm.executeUpdate();
		FactoryConnection.getInstance().closeConexao(con, stm);
		
	}

	@Override
	public ArrayList<Financiamento> getAll() throws SQLException,
			ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlAll);
		ResultSet rs = stm.executeQuery();
		ArrayList<Financiamento> finacimentos = new ArrayList<Financiamento>();
		while(rs.next()){
			finacimentos.add(FinanciamentoUtil.novoFinanciamento(rs));
		}
		FactoryConnection.getInstance().closeConexao(con, stm);
		return finacimentos;
	}

	@Override
	public Financiamento getById(int id) throws SQLException,
			ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlGetByID);
		stm.setInt(1, id);
		ResultSet rs = stm.executeQuery();
		Financiamento financimanto = new Financiamento();
		while(rs.next()){
			financimanto = FinanciamentoUtil.novoFinanciamento(rs);
		}
		FactoryConnection.getInstance().closeConexao(con, stm);
		return financimanto;
	}

}
